import {defineStore} from 'pinia';
import {dynamicRouter} from "@/router";
import {Session} from "@/utils/storage";
import {common} from '@/constance/common'
import router from '@/router'

const useRouterList = defineStore('routerList', {
    state: (): IRouterListState => ({
        menuList: [], // 保存需要缓存的菜单
        toolBarData: [{name: 'Home', path: '/home', title: '首页'}],// 保存标签button的数组
        cacheView: ['BasicLayout', 'ErrorPage'], // 保存需要缓存的数组
        activeView: '/home'
    }),
    getters: {},
    actions: {
        //设置菜单数据
        setMenu() {
            let menuTree = Session.get(common.menuTree);
            let menuList: IMenuItem[] = menuTree as IMenuItem[]
            if (menuList && menuList.length > 0) {
                dynamicRouter(menuList, null);
                this.menuList = menuList;
            }
        },
        //清除数据
        clearAll() {
            this.menuList = []
            this.cacheView = []
        },
        setActiveView(path: string) {
            this.activeView = path
        },
        // 添加标签按钮，如果当前路由已经打开，则不再重复添加
        setToolData(data: any) {
            const inToolbar = this.toolBarData.find(item => item.path === data.path)
            !inToolbar && data.name != 'NoPage404' && this.toolBarData.push({
                ...data
            });
        },
        setCacheView(name: string) { // 与setToolData类似
            if (this.cacheView.includes(name))
                return;
            this.cacheView.push(name);
        },
        removeTab(path: string, type?: string) {
            const index: number = this.toolBarData.findIndex((item) => item.path === path)
            let currentPath: string = router.currentRoute.value.path;
            const currentIndex: number = this.toolBarData.findIndex((item) => item.path === currentPath)
            switch (type) {
                case 'all': //关闭全部标签页
                    this.toolBarData = [{name: 'Home', path: '/home', title: '首页'}]
                    this.cacheView = []
                    router.push({path: '/home'})
                    break
                case 'left': //关闭左侧标签页
                    this.toolBarData.splice(1, index - 1)
                    this.cacheView = this.toolBarData.map(item => item.name)
                    if (currentIndex < index) {
                        this.activeView = path
                        router.push({path})
                    }
                    break
                case 'right': //关闭右侧标签页
                    this.toolBarData.splice(index + 1)
                    this.cacheView = this.toolBarData.map(item => item.name)
                    if (currentIndex > index) {
                        this.activeView = path
                        router.push({path})
                    }
                    break
                case 'other': //关闭其他标签页
                    if (path === '/home') {
                        this.toolBarData = [{name: 'Home', path: '/home', title: '首页'}]
                    } else {
                        this.toolBarData = [{name: 'Home', path: '/home', title: '首页'}, this.toolBarData[index]]
                    }
                    this.cacheView = this.toolBarData.map(item => item.name)
                    if (path !== this.activeView) {
                        this.activeView = path
                        router.push({path})
                    }
                    break
                default: //默认关闭当前标签页
                    this.toolBarData.splice(index, 1)
                    this.cacheView = this.toolBarData.map(item => item.name)
                    if (path == this.activeView) {
                        this.activeView = this.toolBarData[index - 1]['path']
                        router.push({path: this.activeView})
                    }
                    break

            }
        },
    }
})

export default useRouterList;
